﻿using ACC.Business;
using ACC.Business.Stn;
using ACC.Data;
using ACC.Data.ACCLog;
using ACC.Data.Comm;
using ACC.Data.Entities.Common;
using ACC.Data.Entities.Mes;
using ACC.Data.Line;
using ACC.Device;
using ACC.LineDevice.KukaPack;
using iPlant.Features.WOMSClient.Entity;
using System;
using System.Collections.Generic;
using System.Threading;

namespace Script
{
    /// <summary>
    /// EOL测试
    /// OP L1P08020 L1P08021 ... L1P08028
    /// </summary>
    public class L1P08020 : StnProcessKukaPack
    {
        public L1P08020(StnRunningData data)
            : base(data)
        {
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                #region 设置PLC起始地址
                switch (_data.StnCfg.Name)
                {
                    case "L1P08021":
                        kukaSiemensDev.DB190_Offset = 0;
                        break;
                    case "L1P08022":
                        kukaSiemensDev.DB190_Offset = 1000;
                        break;
                    case "L1P08023":
                        kukaSiemensDev.DB190_Offset = 2000;
                        break;
                    case "L1P08024":
                        kukaSiemensDev.DB190_Offset = 3000;
                        break;
                    case "L1P08025":
                        kukaSiemensDev.DB190_Offset = 4000;
                        break;
                    case "L1P08026":
                        kukaSiemensDev.DB190_Offset = 5000;
                        break;
                    case "L1P08027":
                        kukaSiemensDev.DB190_Offset = 6000;
                        break;
                    case "L1P08028":
                        kukaSiemensDev.DB190_Offset = 7000;
                        break;
                }
                #endregion

                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 66);
                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 34);
                kukaSiemensDev.DB193_ACC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 68);
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 34);
                kukaSiemensDev.DB194_PLC_Data_Offset = kukaSiemensDev.DB190_Offset + 78;
            }

            return dev;

        }

        public override ACCCode SendLoadResult(ACCDevice dev, ACCDevice.LoadResult result)
        {
            IsReport = true;  // 使用底层的进站报工处理  
            return base.SendLoadResult(dev, result);
        }

        public override ACCCode SendUnloadResult(ACCDevice dev, ACCDevice.UnloadResult result)
        {
            string msg = "";
            IsReport = false;   //不使用底层的出站报工处理 
            ACCCode code = base.SendUnloadResult(dev, result);
            if (code == ACCCode.ACCOK && result.ErrorCode == ACCCode.ACCOK)
            {
                try
                {
                    var v = _data.LastUnloadUnit.LastHist.Result == WIPUnitHist.UnitHistoryResult.NOK ? UnitStatus.NG :
                        (_data.LastUnloadUnit.Status == WIPUnit.UnitStatus.FG ? UnitStatus.FG : UnitStatus.NG);
                    _womsProcess.ChargeWorkOrder(_data.LastUnloadUnit.UnitSN, "", "", v, out msg);

                    var param = new Dictionary<string, string>();
                    // EOL测试工位过程数据采集
                    param.Clear();
                    param.Add("line", _lineData.Line);
                    param.Add("stn", _data.StnCfg.Name);
                    param.Add("barcode", GetGBSN(_data.LastUnloadUnit.UnitSN, _lineData.Line, "GBSN"));
                    param.Add("unitsn", _data.LastUnloadUnit.UnitSN);

                    new Thread(new ThreadStart(delegate()
                    {
                        var ret = PostWebApiMessage("UploadEOLTestData", "", param);
                        if (ret != null && !ret.IsSuccess)
                            logger.Debug("上传EOL测试数据失败,Error=" + ret.Msg);
                    })).Start();
                }
                catch (Exception ex)
                {
                    logger.Error(ex.StackTrace);
                }
                StnEventLog.Log(_data, StnLog.LogType.Error,
                        string.Format("出站报工{0} UnitSN={1}, ErrorMessage={2}", "失败", _data.LastUnloadUnit.UnitSN, msg));

            }

            return code;
        }
      
    }
}